package com.cpi.cornpp.costelement.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.common.filter.BreadCrumbList;
import com.cpi.cornpp.costelement.exception.CostElementException;
import com.cpi.cornpp.costelement.form.CostElementForm;
import com.cpi.cornpp.costelement.view.CostElementView;
import com.cpi.cornpp.transports.view.TransportView;
import com.cs.common.init.beans.SiteMapDTO;

public class AddCostElementTransportAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String COST_ELEMENT_LIST_FORWARD = "costElementList";
	private static final String SHOW_NEW_COST_ELEMENT_FORWARD = "showNewCostElement";
	private static final String SHOW_EDIT_COST_ELEMENT_FORWARD = "showEditCostElement";
	private static final String FAILURE_FORWARD = "error";

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws CostElementException {
		int selectedTransportId = 0;
		double selectedTransportAmount = 0.0D;
		CostElementForm costElementForm = null;
		CostElementView costElementView = null;
		TransportView transportView = null;
		List transportsView = null;
		List addedCostElementTransports = null;
		HttpSession session = null;
		ActionForward forward = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		
		try{
			
			session = request.getSession(false);
			costElementView = (CostElementView) session.getAttribute("costElementView");
			transportsView = (List) session.getAttribute("transportsList");
			addedCostElementTransports = (List) session.getAttribute("addedCostElementTransport");
			
			if ((form != null) && (costElementView != null)){
				costElementForm = (CostElementForm) form;
				selectedTransportId = costElementForm.getTransportView().getTransportIdentifier();
				selectedTransportAmount = costElementForm.getTransportView().getTransportCost();
				
				addedCostElementTransports = (CollectionUtils.isNotEmpty(addedCostElementTransports)) ? addedCostElementTransports : new ArrayList(0);
				
				if (CollectionUtils.isNotEmpty(transportsView)){
					
					for (Iterator itr = transportsView.iterator(); itr.hasNext(); ){
						transportView = (TransportView) itr.next();
						
						if (transportView.getTransportIdentifier() == selectedTransportId){
							transportView.setTransportCost(selectedTransportAmount);
							costElementView.getTransports().add(transportView);
							addedCostElementTransports.add(transportView);
							break;
						}
					}
				}
				
				session.setAttribute("costElementView", costElementView);
				session.setAttribute("addedCostElementTransport", addedCostElementTransports);
				request.setAttribute("costElementView", costElementView);
				
				forward = (costElementForm.getCostElementView().getCostElementID() != -1) ? mapping.findForward(SHOW_EDIT_COST_ELEMENT_FORWARD) : mapping.findForward(SHOW_NEW_COST_ELEMENT_FORWARD);
				
				//FIXME
				BreadCrumbList breadCrumbList = new BreadCrumbList(0);
				SiteMapDTO siteMapDTO = new SiteMapDTO();
				
				siteMapDTO.setLabel("label.breadcrum.home");
				siteMapDTO.setUrl("welcome.do");
				breadCrumbList.add(siteMapDTO);
				
				siteMapDTO = new SiteMapDTO();
				
				siteMapDTO.setLabel("label.breadcrum.transport.list");
				siteMapDTO.setUrl("showTransportQuery.do");
				breadCrumbList.add(siteMapDTO);
				
				breadCrumbList.setLabel((costElementForm.getCostElementView().getCostElementID() != -1) ? "label.breadcrum.costElement.edit" : "label.breadcrum.costElement.new");
				
				session.setAttribute("BREADCRUMBS", breadCrumbList);
				session.setAttribute("BREADCRUMB_LABEL", breadCrumbList.getLabel());
			} else {
				forward = mapping.findForward(COST_ELEMENT_LIST_FORWARD);
			}
			
		} catch(Exception e){
			CostElementException cee = new CostElementException(e , CostElementException.LAYER_ACTION, CostElementException.ACTION_INSERT);;
			
			session.setAttribute(CUSTOM_EXCEPTION, cee);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw cee;
		} finally {
			session.removeAttribute("transportsList");
		}
		
		return forward;
	}
}